using System;
using System.Text;
namespace CommandLib.Base
{
	public class ByteHelper
	{
		public static void UIntToByte2(ushort i, ref byte bt1, ref byte bt2)
		{
			bt1 = Convert.ToByte((65280 & i) >> 8);
			bt2 = Convert.ToByte((int)(255 & i));
		}
		public static void UIntToByte2(ushort i, ref byte[] bts, int offset)
		{
			bts[offset] = Convert.ToByte((65280 & i) >> 8);
			bts[1 + offset] = Convert.ToByte((int)(255 & i));
		}
		public static void UIntToByte4(uint i, ref byte bt1, ref byte bt2, ref byte bt3, ref byte bt4)
		{
			byte[] bytes = BitConverter.GetBytes(i);
			bt1 = bytes[3];
			bt2 = bytes[2];
			bt3 = bytes[1];
			bt4 = bytes[0];
		}
		public static void UIntToByte4(uint i, byte[] bts, int offset)
		{
			byte[] bytes = BitConverter.GetBytes(i);
			for (int j = 0; j < 4; j++)
			{
				bts[j + offset] = bytes[3 - j];
			}
		}
		public static void IntToByte2(short Val, byte[] bts, int offset)
		{
			byte[] bytes = BitConverter.GetBytes(Val);
			for (short num = 0; num < 2; num += 1)
			{
				bts[(int)num + offset] = bytes[(int)(1 - num)];
			}
		}
		public static void IntToByte4(int Val, byte[] bts, int offset)
		{
			byte[] bytes = BitConverter.GetBytes(Val);
			for (int i = 0; i < 4; i++)
			{
				bts[i + offset] = bytes[3 - i];
			}
		}
		public static short Byte2ToInt(byte[] bts, int offset)
		{
			byte[] array = new byte[2];
			for (int i = 0; i < 2; i++)
			{
				array[1 - i] = bts[i + offset];
			}
			return BitConverter.ToInt16(array, 0);
		}
		public static ushort Byte2ToUInt(byte bt1, byte bt2)
		{
			return (ushort)((int)bt1 << 8 | (int)bt2);
		}
		public static ulong Byte8ToULong(byte bt1, byte bt2, byte bt3, byte bt4, byte bt5, byte bt6, byte bt7, byte bt8)
		{
			return BitConverter.ToUInt64(new byte[]
			{
				bt8, 
				bt7, 
				bt6, 
				bt5, 
				bt4, 
				bt3, 
				bt2, 
				bt1
			}, 0);
		}
		public static void ULongToByte8(ulong i, byte[] bts, int offset)
		{
			ByteHelper.ULongToByte8(i, ref bts[offset], ref bts[offset + 1], ref bts[offset + 2], ref bts[offset + 3], ref bts[offset + 4], ref bts[offset + 5], ref bts[offset + 6], ref bts[offset + 7]);
		}
		public static void ULongToByte8(ulong i, ref byte bt1, ref byte bt2, ref byte bt3, ref byte bt4, ref byte bt5, ref byte bt6, ref byte bt7, ref byte bt8)
		{
			byte[] bytes = BitConverter.GetBytes(i);
			bt1 = bytes[7];
			bt2 = bytes[6];
			bt3 = bytes[5];
			bt4 = bytes[4];
			bt5 = bytes[3];
			bt6 = bytes[2];
			bt7 = bytes[1];
			bt8 = bytes[0];
		}
		public static uint Byte4ToUInt(byte bt1, byte bt2, byte bt3, byte bt4)
		{
			return BitConverter.ToUInt32(new byte[]
			{
				bt4, 
				bt3, 
				bt2, 
				bt1
			}, 0);
		}
		public static int Byte4ToInt(byte bt1, byte bt2, byte bt3, byte bt4)
		{
			byte[] value = new byte[]
			{
				bt4, 
				bt3, 
				bt2, 
				bt1
			};
			return BitConverter.ToInt32(value, 0);
		}
		public static ushort Byte2ToUInt(byte[] bts, int offset)
		{
			return ByteHelper.Byte2ToUInt(bts[offset], bts[offset + 1]);
		}
		public static void FloatToByte4(string str, byte[] bts, int offset)
		{
			ByteHelper.FloatToByte4(Convert.ToSingle(str), bts, offset);
		}
		public static void FloatToByte4(float val, byte[] bts, int offset)
		{
			byte[] bytes = BitConverter.GetBytes(val);
			for (int i = 0; i < 4; i++)
			{
				bts[i + offset] = bytes[3 - i];
			}
		}
		public static float Byte4ToFloat(byte[] bts, int offset)
		{
			byte[] array = new byte[4];
			for (int i = 0; i < 4; i++)
			{
				array[3 - i] = bts[i + offset];
			}
			return BitConverter.ToSingle(array, 0);
		}
		public static void DoubleToByte8(double val, byte[] bts, int offset)
		{
			byte[] bytes = BitConverter.GetBytes(val);
			for (int i = 0; i < 8; i++)
			{
				bts[i + offset] = bytes[7 - i];
			}
		}
		public static double Byte8ToDouble(byte[] bts, int offset)
		{
			byte[] array = new byte[8];
			for (int i = 0; i < 8; i++)
			{
				array[7 - i] = bts[i + offset];
			}
			return BitConverter.ToDouble(array, 0);
		}
		public static byte[] StringToGBK(string str)
		{
			byte[] result;
			if (string.IsNullOrEmpty(str))
			{
				result = new byte[0];
			}
			else
			{
				result = Encoding.GetEncoding("GBK").GetBytes(str);
			}
			return result;
		}
		public static int FillSGBK(string str, byte[] bts, int offset, int length)
		{
			int num = 0;
			if (str != null)
			{
				byte[] array = ByteHelper.StringToGBK(str);
				for (int i = 0; i < array.Length; i++)
				{
					bts[offset + i] = array[i];
				}
				num = array.Length;
			}
			for (int i = num; i < length; i++)
			{
				bts[offset + i] = 0;
			}
			return (num > length) ? num : length;
		}
		public static string GBKToString(byte[] bts, int offset)
		{
			return ByteHelper.SGBKToString(bts, offset, bts.Length - offset);
		}
		public static string SGBKToString(byte[] bts, int offset, int count)
		{
			return Encoding.GetEncoding("GBK").GetString(bts, offset, count).Replace("\0", "");
		}
		public static byte[] HexStringToBytes(string hex)
		{
			int num = hex.Length / 2;
			byte[] array = new byte[num];
			for (int i = 0; i < num; i++)
			{
				int num2 = i * 2;
				byte[] arg_4A_0 = array;
				int arg_4A_1 = i;
				char c = hex[num2];
				string arg_3E_0 = c.ToString();
				c = hex[num2 + 1];
				arg_4A_0[arg_4A_1] = Convert.ToByte(arg_3E_0 + c.ToString(), 16);
			}
			return array;
		}
		public static string BytesToHexString(byte[] bts)
		{
			return ByteHelper.BytesToHexString(bts, 0, bts.Length);
		}
		public static string BytesToHexString(byte[] bts, int offset, int count)
		{
			StringBuilder stringBuilder = new StringBuilder(bts.Length * 2);
			for (int i = 0; i < count; i++)
			{
				stringBuilder.Append(bts[i + offset].ToString("X2"));
			}
			return stringBuilder.ToString();
		}
		public static int Base64StrToInt(string strBase64)
		{
			byte[] array = Convert.FromBase64String(strBase64);
			return ByteHelper.Byte4ToInt(array[0], array[1], array[2], array[3]);
		}
	}
}
